/**
 * 
 */
package com.tomagency.cyberlab.client.activity;

import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.ServerFailure;
import com.google.web.bindery.requestfactory.shared.Violation;
import com.tomagency.cyberlab.client.ClientFactory;
import com.tomagency.cyberlab.client.events.BelegEvent;
import com.tomagency.cyberlab.client.events.KontaktEvent;
import com.tomagency.cyberlab.client.events.ObjectEvent;
import com.tomagency.cyberlab.client.place.ObjectListenPlace;
import com.tomagency.cyberlab.client.ui.ObjectListenView;
import com.tomagency.cyberlab.client.ui.ObjectListenView.ObjectEventHandler;
import com.tomagency.cyberlab.client.ui.ObjectListenView.Presenter;

import de.gammadata.gwt.proxy.BelegListProxy;
import de.gammadata.gwt.proxy.KontaktListProxy;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author gfr
 *
 */
public class KontaktListenActivity extends AbstractListenActivity  implements Presenter<KontaktEvent>,ObjectEventHandler<KontaktEvent>{
	
	

	public KontaktListenActivity(ObjectListenPlace place,ObjectListenView view,ClientFactory clientFactory) {
		super(place, view, clientFactory);
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.tomagency.cyberlab.client.activity.AbstractListenActivity#holeListeVomServer()
	 */
	@Override
	public void holeListeVomServer(){
		if (isDataFetchinProgress())
			return;
//		searchTxt.setText("");
		getView().setMessage("hole Daten vom Server ......");
//		errorLabel.setText("hole Daten vom Server");
		setDataFetchinProgress(true);
		
		getClientFactory().getRpcServiceProvider().getRequestFactory().kontaktRequest().findAll(getStart(),getLimit()).fire(
				new Receiver<List>(){

					@Override
					public void onSuccess(List response) {	
						setDataFetchinProgress(false);
						onListRequestCompleted(response);
						
					}


					@Override
					public void onFailure(ServerFailure error) {
						setDataFetchinProgress(false);
						super.onFailure(error);
					}

					@Override
					public void onViolation(Set<Violation> errors) {
						setDataFetchinProgress(false);
						super.onViolation(errors);
					}
					
				}
				);
	}



	/* (non-Javadoc)
	 * @see com.tomagency.cyberlab.client.activity.AbstractListenActivity#sucheBelege(java.lang.String)
	 */
	@Override
	public void sucheObjekte(String suchText){
		if (isDataFetchinProgress())
			return;
		setDataFetchinProgress(true);
		setLastSuchText(suchText);
		getView().setMessage("suche nach: "+suchText+", hole Daten vom Server");
		getClientFactory().getRpcServiceProvider().getRequestFactory().kontaktRequest().findByMatchString(suchText).fire(
				new Receiver<List>(){

					@Override
					public void onSuccess(List response) {	
						
						onSearchRequestCompleted(response);
						setDataFetchinProgress(false);
					}
					
					@Override
					public void onFailure(ServerFailure error) {
						setDataFetchinProgress(false);
						super.onFailure(error);
					}

					@Override
					public void onViolation(Set<Violation> errors) {
						setDataFetchinProgress(false);
						super.onViolation(errors);
					}
					
				}
				);
	}
	
	/* (non-Javadoc)
	 * @see com.tomagency.cyberlab.client.activity.AbstractListenActivity#onObjectClicked(int, java.lang.Object)
	 */
	@Override
	public void onObjectClicked(int rowNr, int id) {
		setCurrentRowNr(rowNr);
//		CyKontaktListProxy kontakt = (CyKontaktListProxy)clickedObject;
//		Window.alert("Zeile: "+rowNr+" Beleg: "+beleg.getNummer());		
		getClientFactory().getRpcServiceProvider().getRequestFactory().kontaktRequest().findFull(id).fire(  //.with(....)
					new Receiver<KontaktListProxy>(){

						@Override
						public void onSuccess(KontaktListProxy beleg) {
							onObjectRequestCompleted(beleg,getCurrentRowNr());
//							renderListRow(beleg,selectedRow,RenderType.FULL);
//							Window.alert("Beleg geholt: "+beleg.getUuid()+ ", "+beleg.getSubject());

						}
						
					}
					);	
	}
	
	public void onObjectEvent(KontaktEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setPresenterInView(ObjectListenView view) {
		view.setPresenter(this);
		view.setObjectEventHandler(this);
		
	}
	
}//Ende der Classe

